﻿//----------------------------------------------------------------------------------------------------------------------------
//  Version | DateTime(Create Or Update)  |  UserName		|  Description	|	Copyright (c) WEI.ZHOU (Willis). All rights reserved.					                  |
//----------------------------------------------------------------------------------------------------------------------------
//  1.0		| 2025/7/13 17:34:42		  | WEI.ZHOU (Willis)	|  Create RedisHelperTest Class
//----------------------------------------------------------------------------------------------------------------------------

using StackExchange.Redis;

namespace Common.Redis.Test;

[TestClass]
public sealed class RedisHelperTest
{
  [TestMethod]
  public void TestString()
  {
    using (var redisHelper = new RedisHelper("localhost:6379", "Common.Redis"))
    {
      redisHelper.StringSet("Main:Name", "Hello World");
      var value = redisHelper.StringGet<string>("Main:Name");
      Assert.AreEqual("Hello World", value); // True
    }
  }

  [TestMethod]
  public void TestStringSetAndGet()
  {
    using (var redisHelper = new RedisHelper("localhost:6379", "Common.Redis"))
    {
      var key = "TestKey";
      var value = "TestValue";

      // 设置值
      var setResult = redisHelper.StringSet(key, value);
      Assert.IsTrue(setResult);

      // 获取值
      var getResult = redisHelper.StringGet(key);
      Assert.AreEqual(value, getResult);
    }
  }

  [TestMethod]
  public async Task TestStringSetAndGetAsync()
  {
    using (var redisHelper = new RedisHelper("localhost:6379", "Common.Redis"))
    {
      var key = "TestKeyAsync";
      var value = "TestValueAsync";

      // 异步设置值
      var setResult = await redisHelper.StringSetAsync(key, value);
      Assert.IsTrue(setResult);

      // 异步获取值
      string getResult = await redisHelper.StringGetAsync(key, null);
      Assert.AreEqual(value, getResult);
    }
  }

  [TestMethod]
  public void TestStringSetAndGetWithObject()
  {
    using (var redisHelper = new RedisHelper("localhost:6379", "Common.Redis"))
    {
      string key = "TestObjectKey";
      var obj = new StudentEntiy() { Name = "John", Age = 30 };

      // 设置对象
      bool setResult = redisHelper.StringSet(key, obj);
      Assert.IsTrue(setResult);

      // 获取对象
      var resultObj = redisHelper.StringGet<StudentEntiy>(key);
      Assert.IsNotNull(resultObj);
      Assert.AreEqual(obj.Name, resultObj.Name);
      Assert.AreEqual(obj.Age, resultObj.Age);
    }
  }

  [TestMethod]
  public void TestHashSetAndHashGet()
  {
    using (var redisHelper = new RedisHelper("localhost:6379", "Common.Redis"))
    {
      string key = "TestHashKey";
      string field = "TestField";
      string value = "TestValue";

      // 设置哈希字段
      bool setResult = redisHelper.HashSet(key, field, value);
      Assert.IsTrue(setResult);

      // 获取哈希字段
      RedisValue getResult = redisHelper.HashGet(key, field);
      Assert.AreEqual(value, getResult.ToString());
    }
  }

  [TestMethod]
  public void TestListOperations()
  {
    using (var redisHelper = new RedisHelper("localhost:6379", "Common.Redis"))
    {
      string key = "TestListKey";
      string value = "TestListItem";

      // 右侧推入
      long pushResult = redisHelper.ListRightPush(key, value);
      Assert.IsTrue(pushResult > 0);

      // 左侧弹出
      string popResult = redisHelper.ListLeftPop(key);
      Assert.AreEqual(value, popResult);
    }
  }

  [TestMethod]
  public void TestSortedSetOperations()
  {
    using (var redisHelper = new RedisHelper("localhost:6379", "Common.Redis"))
    {
      string key = "TestSortedSetKey";
      string member = "Member1";
      double score = 10;

      // 添加成员
      bool addResult = redisHelper.SortedSetAdd(key, member, score);
      Assert.IsTrue(addResult);

      // 获取长度
      long length = redisHelper.SortedSetLength(key);
      Assert.AreEqual(1, length);

      // 删除成员
      bool removeResult = redisHelper.SortedSetLength(key, member); // 注意：该方法实际是删除
      Assert.IsTrue(removeResult);
    }
  }

  [TestMethod]
  public void TestKeyOperations()
  {
    using (var redisHelper = new RedisHelper("localhost:6379", "Common.Redis"))
    {
      string key = "TestKeyForDelete";

      // 设置一个键
      redisHelper.StringSet(key, "value");

      // 检查是否存在
      bool exists = redisHelper.KeyExists(key);
      Assert.IsTrue(exists);

      // 删除键
      bool deleteResult = redisHelper.KeyDelete(key);
      Assert.IsTrue(deleteResult);

      // 再次检查是否存在
      exists = redisHelper.KeyExists(key);
      Assert.IsFalse(exists);
    }
  }

  [TestMethod]
  public void TestPublishSubscribe()
  {
    /*using (var redisHelper = new RedisHelper("localhost:6379", "Common.Redis", 1))
    {
      RedisChannel channel = "TestChannel";
      string message = "Hello Redis Pub/Sub";
      bool received = false;

      // 订阅消息
      redisHelper.Subscribe(channel, (ch, msg) =>
      {
        Assert.AreEqual(message, msg.ToString());
        received = true;
      });

      // 发布消息
      long count = redisHelper.Publish(channel, message);
      Assert.IsTrue(count > 0);

      // 等待接收
      System.Threading.Thread.Sleep(1000);
      Assert.IsTrue(received);
    }*/
  }
}